package com.bocloud.cmp.driver.providers.impl;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.driver.config.Configuration;
import com.bocloud.cmp.driver.providers.SubnetDriver;
import com.bocloud.cmp.driver.providers.ali.AliSubnetDriver;
import com.bocloud.cmp.driver.providers.intf.SubnetProvider;
import com.bocloud.cmp.driver.providers.jd.JdSubnetDriver;
import com.bocloud.cmp.driver.providers.os.OSSubnetDriver;
import com.bocloud.cmp.driver.providers.os.OSDriver.Version;
import com.bocloud.cmp.driver.utils.VendorConverter;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.model.Butler;
import com.bocloud.cmp.model.SubnetModel;
import com.bocloud.common.model.BsmResult;

@Service("subnetProvider")
public class SubnetProviderImpl implements SubnetProvider {

	@Override
	public BsmResult list(String vendor, String region) {
		SubnetDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSubnetDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			break;
		case ALIYUN:
			driver = new AliSubnetDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			break;
		case OPENSTACK:
			driver = new OSSubnetDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			break;
		case VMWARE:

			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.list();
		}
	}

	@Override
	public BsmResult create(String vendor, String region, SubnetModel subnet) {
		BsmResult result = null;
		SubnetDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSubnetDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.create(subnet);
			break;
		case ALIYUN:
			driver = new AliSubnetDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.create(subnet);
			break;
		case OPENSTACK:
			driver = new OSSubnetDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.create(subnet);
			break;
		case VMWARE:

			break;
		default:
			result = new BsmResult(false, "NOT_SUPPORTED");
			break;
		}
		return result;
	}

	@Override
	public BsmResult remove(String subnetId, String vendor, String region) {
		BsmResult result = null;
		SubnetDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSubnetDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.remove(subnetId);
			break;
		case ALIYUN:
			driver = new AliSubnetDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.remove(subnetId);
			break;
		case OPENSTACK:
			driver = new OSSubnetDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.remove(subnetId);
			break;
		case VMWARE:

			break;
		default:
			result = new BsmResult(false, "NOT_SUPPORTED");
			break;
		}
		return result;
	}

	@Override
	public BsmResult detail(String subnetId, String vendor, String region) {
		BsmResult result = null;
		SubnetDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSubnetDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.detail(subnetId);
			break;
		case ALIYUN:
			driver = new AliSubnetDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.detail(subnetId);
			break;
		case OPENSTACK:
			driver = new OSSubnetDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.detail(subnetId);
			break;
		case VMWARE:

			break;
		default:
			result = new BsmResult(false, "NOT_SUPPORTED");
			break;
		}
		return result;
	}

}
